﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ApplLogic.DbToApplConvertor;
using DAL;
using ApplLogic.ApplClasses;
using Entities;

namespace ApplLogic.ApplDbMediators
{
    public class ApplDbWorkerMediator
    {
        private DataService m_DataService = DataService.Instance;

        private static volatile ApplDbWorkerMediator s_Instance = null;
        private static object s_InstanceLockObj = new Object();

        private ApplDbWorkerMediator() { }

        public static ApplDbWorkerMediator Instance
        {
            get
            {
                if (s_Instance == null)
                {
                    lock (s_InstanceLockObj)
                    {
                        if (s_Instance == null)
                        {
                            s_Instance = new ApplDbWorkerMediator();
                        }
                    }
                }

                return s_Instance;
            }
        }

        public void AddWorker(ApplWorker workerToAdd, bool saveChanges = false)
        {
            Worker dbWorker = DbApplObjectConvertor.CreateDbWorkerFromApplWorker(workerToAdd);
            m_DataService.AddWorker(dbWorker, saveChanges);
        }

        public void DeleteWorker(string workerId, bool saveChanges = false)
        {
            m_DataService.DeleteWorker(workerId, saveChanges);
        }

        public ApplWorker GetWorker(string workerId)
        {
            Worker dbWorker = m_DataService.GetWorkerByID(workerId);
            return DbApplObjectConvertor.CreateApplWorkerFromDbWorker(dbWorker);
        }

        public ApplWorker GetWorker(string userName, string password)
        {
            Worker dbWorker = m_DataService.GetWorkerByUserNameAndPassword(userName, password);
            return dbWorker != null ? DbApplObjectConvertor.CreateApplWorkerFromDbWorker(dbWorker) : null;
        }

        public List<ApplWorker> GetAllWorkers()
        {
            List<ApplWorker> allWorkers = new List<ApplWorker>();
            foreach (Worker worker in m_DataService.GetAllWorkers())
            {
                allWorkers.Add(DbApplObjectConvertor.CreateApplWorkerFromDbWorker(worker));
            }
            return allWorkers;
        }

        public List<string> GetAllWorkersIds()
        {
            List<string> allWorkersIds = new List<string>();
            foreach (Worker worker in m_DataService.GetAllWorkers())
            {
                ApplWorker applWorker = DbApplObjectConvertor.CreateApplWorkerFromDbWorker(worker);
                allWorkersIds.Add(applWorker.PersonalDetails.Id);
            }
            return allWorkersIds;
        }

        public void UpdateWorkerName(string id, string newName, bool saveChanges = false)
        {
            m_DataService.UpdateWorkerName(id, newName, saveChanges);
        }

        public void UpdateWorkerLastName(string id, string newLastName, bool saveChanges = false)
        {
            m_DataService.UpdateWorkerLastName(id, newLastName, saveChanges);
        }

        public void UpdateWorkerEmailAdress(string id, string emailAdress, bool saveChanges = false)
        {
            m_DataService.UpdateWorkerEmailAdress(id, emailAdress, saveChanges);
        }

        public void UpdateWorkerBirthDate(string id, DateTime newBirthDate, bool saveChanges = false)
        {
            m_DataService.UpdateWorkerBirthDate(id, newBirthDate, saveChanges);
            
        }

        public void UpdateWorkerUserName(string id, string newUserName, bool saveChanges = false)
        {
            m_DataService.UpdateWorkerUserName(id, newUserName, saveChanges);
        }

        public void UpdateWorkerPassword(string id, string newPassword, bool saveChanges = false)
        {
            m_DataService.UpdateWorkerPassword(id, newPassword, saveChanges);
        }

        public void UpdateWorkerStartOfWork(string id, DateTime newStartOfWork, bool saveChanges = false)
        {
            m_DataService.UpdateWorkerStartOfWork(id, newStartOfWork, saveChanges);

        }

        public void UpdateWorkerMaxNumberOfShifts(string id, int newMaxNumberOfShifts, bool saveChanges = false)
        {
            m_DataService.UpdateWorkerMaxNumberOfShifts(id, newMaxNumberOfShifts, saveChanges);
        }

        public void UpdateWorker(ApplWorker applUpdatedWorker)
        {
            UpdateWorkerName(applUpdatedWorker.PersonalDetails.Id, applUpdatedWorker.PersonalDetails.FirstName);
            UpdateWorkerLastName(applUpdatedWorker.PersonalDetails.Id, applUpdatedWorker.PersonalDetails.LastName);
            UpdateWorkerEmailAdress(applUpdatedWorker.PersonalDetails.Id, applUpdatedWorker.PersonalDetails.EmailAdress);
            UpdateWorkerBirthDate(applUpdatedWorker.PersonalDetails.Id, applUpdatedWorker.PersonalDetails.BirthDate);
            UpdateWorkerUserName(applUpdatedWorker.PersonalDetails.Id, applUpdatedWorker.PersonalDetails.UserName);
            UpdateWorkerPassword(applUpdatedWorker.PersonalDetails.Id, applUpdatedWorker.PersonalDetails.Password);
            UpdateWorkerStartOfWork(applUpdatedWorker.PersonalDetails.Id, applUpdatedWorker.StartOfWork);
            UpdateWorkerMaxNumberOfShifts(applUpdatedWorker.PersonalDetails.Id, applUpdatedWorker.MaxShiftsThatCanGive);
            m_DataService.SaveChanges();
        }

        //public void DeleteAllRequestsOfWorkerInDateRange(string workerId, DateTime startTime, DateTime endTime, bool saveChanges = false)
        //{
        //    m_DataService.DeleteAllRequestsForWorkerInDateRange(workerId, startTime, endTime, saveChanges);
        //}


    }
}
